package com.it.flowable.model;

import com.it.flowable.listener.AdvancedFlowableListener;
import com.it.flowable.util.*;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.it.flowable.model.FourLevelModel.EXECUTION_LISTENER;
import static com.it.flowable.util.ExtensionElementUtils.createExtensionAttribute;
import static com.it.flowable.util.FieldExtensionUtils.createFieldExtension;

public class LeaveProcessModel {

    // 流程常量
    public static final String PROCESS_KEY = "leaveProcess";
    public static final String PROCESS_NAME = "请假审批流程";

    // 节点ID
    public static final String START_EVENT = "start";
    public static final String APPLY_TASK = "applyTask";
    public static final String LEADER_APPROVAL = "leaderApproval";
    public static final String END_EVENT = "end";

    public static final List<FormProperty> SUBMIT_FORM_PROPERTIES = DataUtils.createSubmitFormProperties();
    public static final List<FormProperty> APPROVAL_FORM_PROPERTIES = DataUtils.createApprovalFormProperties();

    public static final Map<String, List<ExtensionAttribute>> ATTRIBUTES = createAttributes();

    public static final List<FlowableListener> EXECUTION_LISTENERS = createExecutionListener();

    public static final List<FormProperty> FORM_PROPERTIES = createFormProperty();

    public static final Map<String, List<ExtensionElement>> EXTENSION_ELEMENTS = createExtensionElement();

    // Java 8 简化版测试数据
    public static Map<String, List<ExtensionAttribute>> createAttributes() {
        Map<String, List<ExtensionAttribute>> attributes = new HashMap<>();

        // 为不同命名空间创建属性
        String namespace1 = "http://flowable.org/bpmn";
        String namespace2 = "http://custom.org/bpmn";

        // 命名空间1的属性
        List<ExtensionAttribute> ns1Attributes = Arrays.asList(
                createExtensionAttribute("priority", "high"),
                createExtensionAttribute("async", "true")
        );

        // 命名空间2的属性
        List<ExtensionAttribute> ns2Attributes = Arrays.asList(
                createExtensionAttribute("businessUnit", "sales"),
                createExtensionAttribute("region", "north")
        );

        attributes.put(namespace1, ns1Attributes);
        attributes.put(namespace2, ns2Attributes);

        return attributes;
    }

    public static List<FlowableListener> createExecutionListener() {
        FlowableListener listener = ListenerUtils.createFlowableListener("start",EXECUTION_LISTENER, ImplementationType.IMPLEMENTATION_TYPE_CLASS);
        FlowableListener listener1 = ListenerUtils.createFlowableListener("end", EXECUTION_LISTENER, ImplementationType.IMPLEMENTATION_TYPE_CLASS);
        return Arrays.asList(listener, listener1);
    }

    public static List<FormProperty> createFormProperty() {
        FormProperty p = FormUtils.createStringProperty("reason", "请假事由", true);
        FormProperty p1 = FormUtils.createStringProperty("comment", "备注", true);
        return Arrays.asList(p, p1);
    }

    public static Map<String, List<ExtensionElement>> createExtensionElement() {
        Map<String, List<ExtensionElement>> extensionElements = new HashMap<>();
        List<Map<String, String>> dataList = AdvancedFlowableListener.MessageType.getMap();
        List<ExtensionElement> list = dataList.stream()
                .map(map -> ExtensionElementUtils.createExtensionElement(map, "notify"))
                .collect(Collectors.toList());
        extensionElements.put("notify", list);
        return extensionElements;
    }


    /**
     * 创建简化的请假审批流程
     */
    public static BpmnModel create() {
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        process.setId(PROCESS_KEY);
        process.setName(PROCESS_NAME);
        model.addProcess(process);

        // 创建并添加所有节点
        Event startEvent = Utils.createStartEvent(START_EVENT, "开始事件", ATTRIBUTES, EXECUTION_LISTENERS, FORM_PROPERTIES, EXTENSION_ELEMENTS);
        UserTask submitTask = Utils.createUserTask(APPLY_TASK, "填写请假申请", "${initiator}", "APPLY", SUBMIT_FORM_PROPERTIES, null, null);
        UserTask leaderTask = Utils.createUserTask(LEADER_APPROVAL, "直属领导审批", "${leaderAssignee}", "APPROVAL", APPROVAL_FORM_PROPERTIES, null, null);
        Event endEvent = Utils.createEndEvent(END_EVENT, "结束事件");

        // 创建序列流
        SequenceFlow flow = Utils.createSequenceFlow(START_EVENT, APPLY_TASK, "提交申请", null, null);
        SequenceFlow flow1 = Utils.createSequenceFlow(APPLY_TASK, LEADER_APPROVAL, "提交审批", null, null);
        SequenceFlow flow2 = Utils.createSequenceFlow(LEADER_APPROVAL, END_EVENT, "审批完成", null, null);

        process.addFlowElement(startEvent);
        process.addFlowElement(submitTask);
        process.addFlowElement(leaderTask);
        process.addFlowElement(endEvent);
        process.addFlowElement(flow);
        process.addFlowElement(flow1);
        process.addFlowElement(flow2);

        return model;
    }


}